﻿using System;
using System.Collections.Generic;
class BinarySignedInt
{
    static short InputNumber()
    {
        short number = 0;
            while (true)
            {
                try
                {
                    number = short.Parse(Console.ReadLine());
                    break;
                }
                catch (OverflowException)
                {
                    Console.Write("number must be in range short (-32768 to 32767).reenter: ");
                }
                catch (FormatException)
                {
                    Console.Write("reenter number. not in wanted format: ");
                }               
            }
        return number;
    }
    static List<int> DecimalToBinary(int number)
    {
        int index = 0;
        List<int> binElement = new List<int>();
        if (number >= 0)
        {
            do
            {
                binElement.Add(number % 2);
                number = number / 2;
                index++;
            } while (number > 0);
            return binElement;
        }
        else
        {
            number = (ushort.MaxValue + number+1);
            for (int j = 0; j < 15; j++)
            {
                binElement.Add(number % 2);
                number = number / 2;
                index++;
            }
            binElement.Add(1);
            return binElement;
        }
    }
    static void Main()
    {
        Console.Write("please input decimal number: ");
        short number = InputNumber();
        List<int> binDigits = DecimalToBinary(number);
        binDigits.Reverse();
        Console.Write("decimal representation of {0} is: ", number);
        for (int i = 0; i < binDigits.Count; i++)
        {
            Console.Write(binDigits[i]);
        }
        Console.ReadLine();
    }
}

